import * as commonService from '../services/report';
import * as datetime from '../utils/datetime'
import { message } from 'antd';
import * as common from '../utils/common'

export default {
    namespace: 'selzbpz',
    state: {
        list: [],
        panels: {},
        tableData: {},
        showEdit: false,
        hideEdit: true,
        unitBase: 10000,
        cyno: '99',
        selected: {},
        hierarchy: "部门.部门.部门",
        sorter: {},
        filters: [],
        visibleFilterName: null,
        month: {
            start: "2017/01",
            end: "2017/07"
        },
        page: {
            index: 1,
            lineNum: 50
        },
        defaultSelected: [111, 112, 113, 121, 122, 123]
    },
    reducers: {
        save(state, { payload: { data: list } }) {
            if (!list) {
                return
            }
            let txId = -1;
            let cbGroups = {};
            let panels = {};
            let tpId = -1;
            let indexs = [];
            let selected = {};
            list.map((m, i) => {
                if (m.TXID !== txId) {
                    cbGroups = {};
                    panels[m.TXID] = {
                        name: m.TXNM,
                        groups: cbGroups
                    };
                    txId = m.TXID;
                }
                if (m.TPID !== tpId) {
                    indexs = new Array();
                    cbGroups[m.TPID] = {
                        name: m.TPNM,
                        indexs: indexs
                    }
                    tpId = m.TPID
                }
                const ds = state.defaultSelected
                if (!selected[m.TPID]) {
                    selected[m.TPID] = []
                }
                for (let j = 0; j < ds.length; j++) {
                    if (m.ID === ds[j]) {
                        selected[m.TPID].push(i)
                        break
                    }
                }
                indexs.push(i)
            })
            return { ...state, list, panels, selected };
        },
        querySucceed(state, { payload: { tableData, page, sorter, filters } }) {
            return { ...state, tableData, page, sorter, filters };
        },
        openModal(state) {
            const showEdit = true;
            return { ...state, showEdit };
        },
        hideEdit(state) {
            const showEdit = false;
            return { ...state, showEdit };
        },
        selectedChange(state, { payload: { tpid, selectedItems } }) {
            const selected = { ...state.selected, [tpid]: selectedItems }
            return { ...state, selected };
        },
        monthStartChange(state, { payload: { start } }) {
            const month = { ...state.month, start }
            return { ...state, month };
        },
        monthEndChange(state, { payload: { end } }) {
            const month = { ...state.month, end }
            return { ...state, month };
        },
        setVisibleFilterName(state, { payload: { visibleFilterName } }) {
            return { ...state, visibleFilterName };
        },
    },
    effects: {
        *fetch({ }, { call, put }) {
            const oData = yield call(commonService.fetchSelIndex);
            if (oData) {
                yield put({
                    type: 'save',
                    payload: {
                        data: oData
                    },
                });
            }
        },
        *query({ payload: { page, pageSize, sorter, filters } }, { call, put, select }) {
            const defaultVals = yield select(state => state.selzbpz);
            if (!page || !pageSize || !sorter || !filters) {
                page = defaultVals.page.index
                pageSize = defaultVals.page.lineNum
                sorter = defaultVals.sorter
                filters = defaultVals.filters
            }
            const { hierarchy, list, selected, month, unitBase } = defaultVals
            var measures = []
            Object.keys(selected).map(tpid => {
                const indexIds = selected[tpid]
                const meas = indexIds.map(ind => {
                    return String(list[ind].ID)
                })
                measures.push(...meas)
            })
            const monthStart = common.formatMonth(month.start)
            const monthEnd = common.formatMonth(month.end)
            let sorts=[]
            if (sorter&&sorter.column){
                sorts.push({
                    type: sorter.column.type,
                    column: sorter.field,
                    order: sorter.order === "ascend" ? "ASC" : "DESC"
                })
            }
            var req = {
                pageIndex: page,
                pageLineNum: pageSize,
                unitBase: unitBase,
                row: {
                    "hierarchy": "部门.部门.部门",
                    "sorts": sorts,
                    "filters": filters
                },
                column: {},
                measures: measures,
                context: [
                    "{" + monthStart + ":" + monthEnd + "}"
                ]
            }
            const tableData = yield call(commonService.queryTableData, req)
            yield put({
                type: 'querySucceed',
                payload: {
                    tableData, page: { index: page, lineNum: pageSize }, sorter, filters
                },
            });
        }
    },
};
